#include <GuiConstants.au3>
#include <Constants.au3>
#include <Array.au3>

#include <globals.au3>
#include <preferences.au3>
#include <reverse-entries-order.au3>
#include <direct-entries-order.au3>

; license
#comments-start
  WinKlipper - A program that backups your clipboard
  Copyright (C) 2006  Janosch Graef

  This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
#comments-end

; Initialization
$clip_entries_size = EntriesNumber()


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; reo_ - reverse entries order functions set  ;;
;; deo_ - direct entries order functions set   ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func OrderFuncPrefix()
     If $GUI_CHECKED == RecentEntriesEasyAccess() Then
        Return "reo_"
     EndIf

     Return "deo_"
EndFunc

Func SaveClipToFile($cb)
    If $GUI_CHECKED == SaveToFileState() Then 
        FileWrite($filename,@MON&"/"&@MDAY&"/"&@YEAR&" "&@HOUR&":"&@MIN&":"&@SEC&":"&@CRLF&$cb&@CRLF&"=========================================="&@CRLF)
    EndIf
EndFunc

; trayeventoptions
func traycbevent($num)
  Call(OrderFuncPrefix() & "traycbevent", $num)
EndFunc

; traycbevent() launchers from menu
func traycbevent1()
	traycbevent(1)
endfunc
func traycbevent2()
	traycbevent(2)
endfunc
func traycbevent3()
	traycbevent(3)
endfunc
func traycbevent4()
	traycbevent(4)
endfunc
func traycbevent5()
	traycbevent(5)
endfunc
func traycbevent6()
	traycbevent(6)
endfunc
func traycbevent7()
	traycbevent(7)
endfunc
func traycbevent8()
	traycbevent(8)
endfunc
func traycbevent9()
	traycbevent(9)
endfunc

func traycbevent10()
	traycbevent(10)
endfunc
func traycbevent11()
	traycbevent(11)
endfunc
func traycbevent12()
	traycbevent(12)
endfunc
func traycbevent13()
	traycbevent(13)
endfunc
func traycbevent14()
	traycbevent(14)
endfunc
func traycbevent15()
	traycbevent(15)
endfunc
func traycbevent16()
	traycbevent(16)
endfunc
func traycbevent17()
	traycbevent(17)
endfunc
func traycbevent18()
	traycbevent(18)
endfunc
func traycbevent19()
	traycbevent(19)
endfunc

func traycbevent20()
	traycbevent(20)
endfunc
func traycbevent21()
	traycbevent(21)
endfunc
func traycbevent22()
	traycbevent(22)
endfunc
func traycbevent23()
	traycbevent(23)
endfunc
func traycbevent24()
	traycbevent(24)
endfunc
func traycbevent25()
	traycbevent(25)
endfunc
func traycbevent26()
	traycbevent(26)
endfunc
func traycbevent27()
	traycbevent(27)
endfunc
func traycbevent28()
	traycbevent(28)
endfunc
func traycbevent29()
	traycbevent(29)
endfunc
func traycbevent30()
	traycbevent(30)
endfunc


; tray
TraySetState()
TraySetToolTip("Click menu entry to put it into clipboard.")

opt("TrayAutoPause",0)
opt("TrayMenuMode",3)
opt("TrayOnEventMode",1)
TraySetClick(18)

    TrayCreateItem("",-1, $clip_entries_size + 2)
TrayCreateItem("",-1, $clip_entries_size + 3)
Dim $tray_prefs = TrayCreateItem("Preferences",-1, $clip_entries_size + 4)
TrayCreateItem("",-1, $clip_entries_size + 5)
Dim $tray_exit = TrayCreateItem("Exit",-1, $clip_entries_size + 6)
TrayItemSetOnEvent($tray_prefs,"Preferences")
TrayItemSetOnEvent($tray_exit,"quit")

; functions
func shorten($text)
	Dim $suffix = 0
	$text = StringReplace($text,@LF,@CRLF)
	$text = StringReplace($text,@CR,@CRLF)	
	Dim $lines = StringSplit($text,@CRLF)
	if (UBound($lines)>2) Then
		$text = $lines[1]
		$suffix = 1
	EndIf
  if (StringLen($text)>64) Then
		$text = StringLeft($text,61)
		$suffix = 1
	endif
	$text = StringStripWS($text, 3)
	if ($suffix==1) then $text = $text&"..."
	return $text
EndFunc

func entry_add()
  Call( OrderFuncPrefix() & "entry_add" )
EndFunc

func quit()
  FileDelete($filename)
  exit
EndFunc

; program
FileDelete($filename)
while TRUE
  $cb = ClipGet()
  if ($cb<>"") Then
	  if ($cb<>$lc) Then
      entry_add()
    endif
  endif
  sleep(500)
wend